/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.quartz.task;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.config.Constant;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * 测试用
 *
 * @author Zheng Jie
 * @date 2019-01-08
 */
@Slf4j
@Component
public class TestTask {

    private static int count = 1;
    private static int count1 = 1;

    public void run() {
        try {

            while (true) {
                if (Constant.lock) {
                    Thread.sleep(100);
                } else {
                    Constant.lock = true;
                    for (int i = 1; i < 18; i++) {
                        Thread.sleep(100);
                        System.out.println(JLibModbus.getValue("127.0.0.1", 502, i, 1));
                    }
                    Constant.lock = false;
                   break;
                }
            }

            while (true) {
                if (Constant.lock) {
                    Thread.sleep(100);
                } else {
                    Constant.lock = true;
                    for (int i = 31; i < 46; i++) {
                        Thread.sleep(100);
                        System.out.println(JLibModbus.getValue("127.0.0.1", 502, i, 1));

                    }
                    Constant.lock = false;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("run 执行成功");
    }

    public void run1(String str) {
        log.info("run1 执行成功，参数为： {}" + str);
    }

    public void run2() {
        try {
            count++;
            for (int i = 1; i < 24; i++) {
                if (i == 2) {
                    JLibModbus.setValue("127.0.0.1", 502, i, new Random().nextInt(1000));
                    continue;
                } else if (i == 11) {
                    JLibModbus.setValue("127.0.0.1", 502, i, new Random().nextInt(100));
                    continue;
                }
                JLibModbus.setValue("127.0.0.1", 502, i, i);
            }
            JLibModbus.setValue("127.0.0.1", 502, 31, 1);
            JLibModbus.setValue("127.0.0.1", 502, 32, 1);
            JLibModbus.setValue("127.0.0.1", 502, 33, 1);

        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("run2 执行成功");
    }

    public void run3() {
        try {
            count++;
            count1++;
            int power = new Random().nextInt(1000) + 1000;
            while (true) {
                if (Constant.lock) {
                    Thread.sleep(100);
                } else {
                    Constant.lock = true;
                    Thread.sleep(100);
                    //新风电量测试01
                    JLibModbus.setValue("127.0.0.1", 502, 31, count);
                    Thread.sleep(100);
                    //新风功率测试02
                    JLibModbus.setValue("127.0.0.1", 502, 32, power);
                    Thread.sleep(100);
                    //新风风量测试03
                    JLibModbus.setValue("127.0.0.1", 502, 33, new Random().nextInt(100));
                    Thread.sleep(100);
                    //排风电量测试01
                    JLibModbus.setValue("127.0.0.1", 502, 34, count);
                    Thread.sleep(100);
                    //排风功率测试02
                    JLibModbus.setValue("127.0.0.1", 502, 35, power);
                    Thread.sleep(100);
                    //排风风量测试02
                    JLibModbus.setValue("127.0.0.1", 502, 36, new Random().nextInt(100));
                    Thread.sleep(100);
                    //照明测试101
                    JLibModbus.setValue("127.0.0.1", 502, 37, count + count1);
                    Thread.sleep(100);
                    //照明测试101功率
                    JLibModbus.setValue("127.0.0.1", 502, 38, new Random().nextInt(100) + 2000);
                    Thread.sleep(100);
                    //空调电量测试01
                    JLibModbus.setValue("127.0.0.1", 502, 39, count * 10);
                    Thread.sleep(100);
                    //空调功率测试01
                    JLibModbus.setValue("127.0.0.1", 502, 40, new Random().nextInt(10000));
                    Thread.sleep(100);
                    //空调风量测试03
                    JLibModbus.setValue("127.0.0.1", 502, 41, new Random().nextInt(500));
                    Thread.sleep(100);
                    //通风101电量
                    JLibModbus.setValue("127.0.0.1", 502, 42, count*3);
                    Thread.sleep(100);
                    //通风101功率
                    JLibModbus.setValue("127.0.0.1", 502, 43, new Random().nextInt(2000));
                    Thread.sleep(100);
                    //通风风量01
                    JLibModbus.setValue("127.0.0.1", 502, 44, new Random().nextInt(100));
                    Thread.sleep(100);
                    //通风风量02
                    JLibModbus.setValue("127.0.0.1", 502, 45, new Random().nextInt(100));
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("run2 执行成功");
    }

    public void realTimeValue()
    {
        try {
            while (true) {
                if (Constant.lock) {
                    Thread.sleep(100);
                } else {
                    Constant.lock = true;
                    Thread.sleep(100);
                    //窗高度
                    JLibModbus.setValue("127.0.0.1", 502, 1, new Random().nextInt(100));
                    Thread.sleep(100);
                    //实时风速
                    JLibModbus.setValue("127.0.0.1", 502, 2, new Random().nextInt(1000));
                    Thread.sleep(100);
                    //实时风量
                    JLibModbus.setValue("127.0.0.1", 502, 3, new Random().nextInt(100));
                    Thread.sleep(100);
                    //阀门开度
                    JLibModbus.setValue("127.0.0.1", 502, 10, new Random().nextInt(100));
                    Thread.sleep(100);
                    //温度
                    JLibModbus.setValue("127.0.0.1", 502, 11, new Random().nextInt(50));
                    Thread.sleep(100);
                    //新风压差
                    JLibModbus.setValue("127.0.0.1", 502, 14, new Random().nextInt(100));
                    Thread.sleep(100);
                    //灯开关
                    JLibModbus.setValue("127.0.0.1", 502, 15, new Random().nextInt(10) > 5 ? 1:0);
                    Thread.sleep(100);
                    //远程开关
                    JLibModbus.setValue("127.0.0.1", 502, 16, new Random().nextInt(10) > 5 ? 1:0);
                    Constant.lock = false;
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void alarmValue() {
        try {
            while (true) {
                if (Constant.lock) {
                    Thread.sleep(100);
                } else {
                    Constant.lock = true;
                    Thread.sleep(100);
                    //风速传感器异常
                    JLibModbus.setValue("127.0.0.1", 502, 4, new Random().nextInt(10) > 5 ? 1:0);
                    Thread.sleep(100);
                    //移动窗过高异常
                    JLibModbus.setValue("127.0.0.1", 502, 5, new Random().nextInt(10) > 5 ? 1:0);
                    Thread.sleep(100);
                    //变风量风阀异常
                    JLibModbus.setValue("127.0.0.1", 502, 6, new Random().nextInt(10) > 5 ? 1:0);
                    Thread.sleep(100);
                    //低风速告警
                    JLibModbus.setValue("127.0.0.1", 502, 8, new Random().nextInt(10) > 5 ? 1:0);
                    Thread.sleep(100);
                    //高风速告警
                    JLibModbus.setValue("127.0.0.1", 502, 9, new Random().nextInt(10) > 5 ? 1:0);
                    Constant.lock = false;
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
